import { defineStore } from 'pinia'
import { handleDefaultSetting, type EnterpriseStore, type Enterprise } from './helper'
import {
  createOrUpdateEnterprise,
  deleteEnterpriseById,
  deleteEnterpriseByIds,
  fetchEnterprise,
  fetchEnterpriseById
} from '@/api'
import type { DictOption } from '@/stores/modules/dict-option/helper'
import { useDictOptionStore } from '../dict-option'

export const useMainInfoStore = defineStore('main-info-store', {
  state: (): EnterpriseStore => handleDefaultSetting(),
  getters: {},
  actions: {
    /**
     * 创建或更新一条数据
     * @param enterprise
     */
    async createOrUpdate(enterprise: Enterprise , $index:number) {
      const { code } = await createOrUpdateEnterprise({ enterprise })
      if (code === 200 && enterprise.id) {
        // If is "modify" the ...
        this.enterpriseList[$index] = enterprise
      }
      return code === 200
    },
    /**
     * 根据id查询企业信息
     * @param id 企业id
     */
    async queryById(id: number) {
      const {
        data: { enterprise }
      } = await fetchEnterpriseById<{ enterprise: Enterprise }>({ id })
      return enterprise
    },
    /**
     * 根据条件查询企业信息
     * @param conditions 查询条件
     */
    async query(conditions?: DictOption) {
      const _object: { [key: string]: any } = {
        current: this.currentPage,
        size: this.pageCount
      }
      for (const key in conditions) {
        const _item = conditions[key]
        if (_item?.value) {
          _object[_item.dictCode] = _item.value
        }
      }
      // code data message
      const {
        data: { enterpriseList, totalCount }
      } = await fetchEnterprise<{ enterpriseList: Array<Enterprise>; totalCount: number }>(_object)

      this.totalCount = totalCount
      this.enterpriseList = enterpriseList
    },
    /**
     * 更新当前页码并重新查询企业信息
     * @param page 当前页码
     */
    async updateCurrentPage(page: number, conditions?: DictOption) {
      this.currentPage = page
      await this.query(conditions)
    },
    /**
     * 更新每页显示数量并重新查询企业信息
     * @param pageCount 每页显示数量
     */
    async updatePageCount(pageCount: number, conditions?: DictOption) {
      this.pageCount = pageCount
      await this.query(conditions)
    },
    /**
     * 根据企业ID删除企业信息
     * @param id 企业ID
     */
    async deleteById(id: number) {
      const { code } = await deleteEnterpriseById<{ code: number }>({ id })
      return code === 200
    },
    /**
     * 根据企业ID列表批量删除企业信息
     * @param ids 企业ID列表
     * @return {boolean} 删除是否成功
     */
    async deleteByIds(ids: Array<number>) {
      const { code } = await deleteEnterpriseByIds<{}>(ids)
      return code === 200
    },
    /**
     * 查询搜索条件
     * @param dictNameList 字典名称列表
     * @return {DictOption} 搜索条件
     */
    async querySearchConditions(dictNameList: Array<string>) {
      const dictOptionStore = useDictOptionStore()
      return await dictOptionStore.query<Enterprise>(dictNameList)
    }
  },
  persist: {
    enabled: true,
    strategies: [
      // {
      //   paths: ['queryConditions'],
      //   storage: sessionStorage
      // },
      {
        paths: ['pageCount', 'showSearchBar'],
        storage: localStorage
      }
    ]
  }
})
